﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Collections;

namespace GRWL.BZSC.UI.CustomCtrl
{
    /// <summary>
    /// 通用选择器
    /// </summary>
    public partial class Selector : UserControl
    {
        private NodeValueType _ValueType = NodeValueType.Value;
        /// <summary>
        /// 属性中显示设置选定值类型
        /// </summary>
        [Browsable(true)]
        [Description("设置选择器选定值类型")]
        [DefaultValue(NodeValueType.Value)]
        public NodeValueType ValueType { get { return this._ValueType; } set { this._ValueType = value; } }

        private Size _ComboBoxSize = new Size(150, 20);
        /// <summary>
        /// 属性中显示设置下拉框大小
        /// </summary>
        [Browsable(true)]
        [Description("设置选择器下拉框大小")]
        [DefaultValue(typeof(Size), "150, 20")]
        public Size ComboBoxSize { get { return this._ComboBoxSize; } set { this._ComboBoxSize = value; } }

        /// <summary>
        /// 选择器当前选择项
        /// </summary>
        public SelectItem CurrentSelectItem
        {
            get;
            set;
        }

        /// <summary>
        /// 选定值类型
        /// </summary>
        public enum NodeValueType
        {
            Key = 1,
            Name,
            Value
        }

        /// <summary>
        /// 获取选定项的值
        /// </summary>
        /// <returns></returns>
        public object SelectedValue()
        {
            try
            {

                if (this.CurrentSelectItem != null)
                {
                    PropertyInfo[] properties = this.CurrentSelectItem.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                    foreach (PropertyInfo item in properties)
                    {
                        if (item.Name.ToLower() == this.ValueType.ToString().ToLower())
                            return item.GetValue(this.CurrentSelectItem, null);
                    }
                }
            }
            catch { }
            return null;
        }

        /// <summary>
        /// 获取选定文本
        /// </summary>
        /// <returns></returns>
        public string SelectedText()
        {
            object o = this.SelectedValue();
            if (o != null)
                return o.ToString();
            return string.Empty;
        }

        /// <summary>
        /// 获取ComboBox文本
        /// </summary>
        /// <returns></returns>
        public string GetComboBoxText()
        {
            return this.comboBox1.Text;
        }

        #region 自定义事件

        public delegate void SelectorEventHandler(object sender, EventArgs e);//事件所需的委托
        [Description("设置选择器按钮点击时触发")]
        public event SelectorEventHandler SelectorClick;//定义一个SelectorClick事件
        protected virtual void OnSelectorClick(EventArgs e)
        {
            if (this.SelectorClick != null)
            { SelectorClick(this, e); }
        }

        public delegate void SelectorTextUpdateEventHandler(object sender, EventArgs e);//SelectorTextUpdate事件所需的委托
        [Description("设置选择器下拉框文本改变时触发")]
        public event SelectorTextUpdateEventHandler SelectorTextUpdate;//定义一个SelectorTextUpdate事件
        protected virtual void OnSelectorTextUpdate(EventArgs e)
        {
            if (this.SelectorTextUpdate != null)
            { SelectorTextUpdate(this, e); }
        }
        #endregion

        /// <summary>
        /// 选择器点击事件参数类型
        /// </summary>        
        public class SelectorEventArgs : EventArgs
        {
            private TreeNode sNode;
            public SelectorEventArgs(TreeNode node)
            {
                sNode = node;
            }
            public TreeNode GetNode
            { get { return sNode; } }
        }

        public Selector()
        {
            InitializeComponent();
            this.comboBox1.ValueMember = "Value";
            this.comboBox1.DisplayMember = "Text";
            this.CurrentSelectItem = null;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            this.comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
            if (this.ComboBoxSize != null && this.ComboBoxSize.Width != 0)
                this.comboBox1.Size = this.ComboBoxSize;
            else
                this.comboBox1.Size = new Size(150, 20);
            base.OnPaint(pe);

        }

        /// <summary>
        /// 设置选定项
        /// </summary>
        /// <param name="item"></param>
        public void SetSelected(SelectItem item)
        {
            this.comboBox1.Items.Clear();
            if (item != null)
            {
                this.comboBox1.Items.Add(item);
                this.comboBox1.ValueMember = "Value";
                this.comboBox1.DisplayMember = "Name";
                this.comboBox1.SelectedItem = item;
            }
            this.CurrentSelectItem = item;
        }

        /// <summary>
        /// 控件ComboBox数据重新绑定
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="results">数据集合</param>
        /// <param name="valueMember">选择器返回类SelectItem的值成员</param>
        /// <param name="displayMember">选择器返回类SelectItem的显示成员</param>
        /// <param name="keyMember">选择器返回类SelectItem的key成员</param>
        public void ReBindComboBox<T>(IEnumerable<T> results, string valueMember, string displayMember, string keyMember)
        {
            this.comboBox1.Items.Clear();
            this.CurrentSelectItem = null;
            if (results != null)
            {
                foreach (T item in results)
                { 
                    Type t = item.GetType();
                    PropertyInfo piValue = t.GetProperty(valueMember);
                    PropertyInfo piDisplay = t.GetProperty(displayMember);
                    PropertyInfo piKey = t.GetProperty(displayMember);
                    SelectItem si = new SelectItem();
                    si.Value = piValue.GetValue(item, null).ToString();
                    si.Name = piDisplay.GetValue(item, null).ToString();
                    si.Key = piKey.GetValue(item, null).ToString();
                    this.comboBox1.Items.Add(si);
                }
                this.comboBox1.ValueMember = "Value";
                this.comboBox1.DisplayMember = "Name";
            }
        }

        private void btnChose_Click(object sender, EventArgs e)
        {
            OnSelectorClick(new EventArgs());
        }

        private void comboBox1_TextUpdate(object sender, EventArgs e)
        {
            OnSelectorTextUpdate(new EventArgs());
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBox1.SelectedItem != null)
                this.CurrentSelectItem =  this.comboBox1.SelectedItem as SelectItem;                            
        }
    }

    /// <summary>
    /// 选择器存值类型
    /// </summary>
    public class SelectItem
    {
        /// <summary>
        /// 选项Code
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// 选项显示的名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 选项的系统Id
        /// </summary>
        public string Value { get; set; }
    }
}